package kth;

import static org.junit.Assert.assertTrue;
import kth.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.snapshot.RobotState;


/**
 * This test verifies that if PacerEvader hits another robot, it will move away from
 * the current wall.
 * 
 * @author Kimberly Heu
 *
 */
public class TestPacerEvaderReaction extends RobotTestBed {
  
  boolean hitRobot = false;
  boolean movedWall = false;

  /**
   * Specifies that SittingDuck and PacerEvader are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,kth.PacerEvader";
  }
  
  /**
   * This test runs for 1 rounds. This is done to guarantee that the robot does not
   * hit a robot in one round and then move away from the wall in a different round.
   * 
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 1;
  }
  
  /**
   * Set the initial positions of SittingDuck and PacerEvader to guarantee
   * a collision.
   * 
   * @return A String containing the position and heading for each of the robots.
   */
  @Override
  public String getInitialPositions() {
    String str = "0," + fieldHeight + ",0, 0,0,0";
    System.out.println(str);
    return str;
  }
  
  /**
   * At the end of each turn, evaluates PacerEvader's status to see if it hit another
   * robot, and, if it did, if PacerEvader moved away from the wall.
   * 
   * @param event details on the turn that just completed.
   */
  @Override 
  public void onTurnEnded(TurnEndedEvent event) {
    
    // Get information on PacerEvader
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    RobotState state = robot.getState();
    double yCoord = robot.getY();
    
    // If PacerEvader hit another robot, then set the appropriate boolean value true.
    if (state.isHitRobot()) {
      hitRobot = true;
    }
    
    // If PacerEvader hit another robot, then make sure it moved away from that wall.
    if (hitRobot && (yCoord > 40 && yCoord < (fieldHeight - 40))) {
      movedWall = true;
    }
  }
  
  
  /**
   * The actual test, which asserts that if PacerEvader hit another robot it also moved
   * to another wall.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Hit a robot and moved to another wall", (!hitRobot) || (hitRobot && movedWall));
    
  }
}
